package com.glq.book.dao;

import com.glq.book.entity.Borrow;
import com.glq.book.util.JdbcUtil;
import com.glq.book.vo.BorrowVO;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BorrowDao {

    private JdbcUtil jdbcUtil = new JdbcUtil();
    private Connection connection = null;
    private ResultSet resultSet = null;
    private PreparedStatement preparedStatement = null;
    private String sql = "";

    /**
     * 借书（添加借书记录）
     *
     * @param borrow
     * @return
     */
    public boolean insertBorrow(BorrowVO borrow) {
        try {
            sql = "insert into " +
                    "borrow(book_name,username,state,borrow_time)" +
                    "values(?,?,?,?)";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, borrow.getBookName());
            preparedStatement.setString(2, borrow.getUsername());
            preparedStatement.setString(3, "0");
            preparedStatement.setDate(4, new Date(System.currentTimeMillis()));
            return preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return true;
    }

    /**
     * 还书（修改借书状态）
     *
     * @param id
     * @return
     */
    public boolean updateBorrow(Integer id) {
        try {
            sql = "update borrow set state=? , repay_time=?" +
                    "where `id` = ?";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "1");
            preparedStatement.setDate(2, new Date(System.currentTimeMillis()));
            preparedStatement.setInt(3, id);
            return preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return true;
    }

    /**
     * 借书记录
     *
     * @param username
     * @return
     */
    public List<Borrow> selectBorrowListByUser(String username) {
        List<Borrow> list = new ArrayList<>();
        try {
            sql = "select * from borrow where username = ? order by borrow_time DESC";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(new Borrow(resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getDate(5),
                        resultSet.getDate(6)
                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return list;
    }

    /**
     * 借书记录
     *
     * @return
     */
    public List<Borrow> selectBorrowList() {
        List<Borrow> list = new ArrayList<>();
        try {
            sql = "select * from borrow order by borrow_time DESC";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(new Borrow(resultSet.getInt(1),
                                resultSet.getString(2),
                                resultSet.getString(3),
                                resultSet.getString(4),
                                resultSet.getDate(5),
                                resultSet.getDate(6)
                        )
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return list;
    }

}
